package com.szh.cloud.api.login.service.impl;

import com.szh.cloud.api.login.entity.SearUserEntity;
import com.szh.cloud.api.login.repository.ApiLoginDao;
import com.szh.cloud.api.login.service.ApiLoginService;
import com.szh.cloud.login.service.LoginService;
import com.szh.cloud.mybatis.entity.PtUserEntity;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ApiLoginServiceIml implements ApiLoginService {
    @Resource
    private ApiLoginDao apiLoginDao;
    @Resource
    private LoginService loginService;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public String userLogin(SearUserEntity entity) throws UsernameNotFoundException {

        PtUserEntity ptUserEntity = loginService.queryUserInfoByName(entity.getUserName());
        if(ptUserEntity == null){
            throw new UsernameNotFoundException(entity.getUserName() + " is not exit.");
        }

        String passwdEn = passwordEncoder.encode(entity.getPassword());

        if(!passwdEn.equals(ptUserEntity.getPassword())){
            throw new UsernameNotFoundException(entity.getUserName() + " is password error.");
        }


        return ptUserEntity.getUserUuid();
    }
}